 /*******************************************************************************
  * Copyright (c) 2000, 2005 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
  * IBM Corporation - initial API and implementation
  *******************************************************************************/
 package org.eclipse.ui.internal.editors.text;

 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.filesystem.IFileStore;

 import org.eclipse.core.runtime.IPath;

 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.wizard.Wizard;

 import org.eclipse.ui.editors.text.EditorsUI;

 import org.eclipse.ui.IEditorDescriptor;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorRegistry;
 import org.eclipse.ui.INewWizard;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PartInitException;

 /**
  * Wizard with no page that creates the untitled text file
  * and opens the text editor.
  *
  * @since 3.1
  */
 public class UntitledTextFileWizard extends Wizard implements INewWizard {

     private IWorkbenchWindow fWindow;

     public UntitledTextFileWizard() {
     }

     /*
      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
      */
     public void dispose() {
         fWindow= null;
     }

     private IFileStore queryFileStore() {
         IPath stateLocation= EditorsPlugin.getDefault().getStateLocation();
         IPath path= stateLocation.append("/_" + new Object ().hashCode()); //$NON-NLS-1$
 return EFS.getLocalFileSystem().getStore(path);
     }

     private String getEditorId(IFileStore fileStore) {
         IWorkbench workbench= fWindow.getWorkbench();
         IEditorRegistry editorRegistry= workbench.getEditorRegistry();
         IEditorDescriptor descriptor= editorRegistry.getDefaultEditor(fileStore.getName());
         if (descriptor != null)
             return descriptor.getId();
         return EditorsUI.DEFAULT_TEXT_EDITOR_ID;
     }

     private IEditorInput createEditorInput(IFileStore fileStore) {
         return new NonExistingFileEditorInput(fileStore, TextEditorMessages.NewTextEditorAction_namePrefix);
     }

     /*
      * @see org.eclipse.jface.wizard.Wizard#performFinish()
      */
     public boolean performFinish() {
         IFileStore fileStore= queryFileStore();
         IEditorInput input= createEditorInput(fileStore);
         String editorId= getEditorId(fileStore);
         IWorkbenchPage page= fWindow.getActivePage();
         try {
             page.openEditor(input, editorId);
         } catch (PartInitException e) {
             EditorsPlugin.log(e);
             return false;
         }
         return true;
     }

     /*
      * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
      */
     public void init(IWorkbench workbench, IStructuredSelection selection) {
         fWindow= workbench.getActiveWorkbenchWindow();
     }
 }

